package rs.fon.neuroph.regression;

import java.io.File;
import java.util.List;

import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.Model;
import com.rapidminer.operator.OperatorCapability;
import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.learner.AbstractLearner;
import com.rapidminer.parameter.ParameterType;
import com.rapidminer.parameter.ParameterTypeFile;
import com.rapidminer.parameter.ParameterTypeInt;

public class NeurophRegressionOperator extends AbstractLearner {

	public static final String PARAMETER_NN_FILE = "nn_file";
	public static final String PARAMETER_MAX_ITERATIONS = "max_iterations";

	public static String nnFilePath;
	public static Integer maxIterations;



	public NeurophRegressionOperator(OperatorDescription description) {
		super(description);
	}

	@Override
	public Model learn(ExampleSet exampleSet) throws OperatorException {

		nnFilePath = getParameterAsString(PARAMETER_NN_FILE);
		maxIterations = getParameterAsInt(PARAMETER_MAX_ITERATIONS);

		if (!(new File(nnFilePath)).exists())
			throw new OperatorException(".nnet file does not exist.");
				
		NeurophRegressionAdapter neuroph = new NeurophRegressionAdapter(maxIterations);
		Model model = neuroph.trainNNModel(nnFilePath, exampleSet);

		return model;
	}

	@Override
	public boolean supportsCapability(OperatorCapability capability) {

		if (capability == com.rapidminer.operator.OperatorCapability.NUMERICAL_ATTRIBUTES)
			return true;
		if (capability == com.rapidminer.operator.OperatorCapability.NUMERICAL_LABEL)
			return true;

		return false;
	}

	public List<ParameterType> getParameterTypes() {
		List<ParameterType> types = super.getParameterTypes();
		ParameterType type;

		type = new ParameterTypeFile(PARAMETER_NN_FILE, "Path to a .nnet file made by Neuroph", "nnet", "");
		type.setExpert(false);
		types.add(type);		

		type = new ParameterTypeInt(PARAMETER_MAX_ITERATIONS, "Max iterations for learning the network", 1, Integer.MAX_VALUE, 1000);
		type.setExpert(false);
		types.add(type);		
		
		return types;
	}

}
